/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.01  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

/* Local headers */
#include "ablibs.h"
#include "abimport.h"
#include "proto.h"

#include "../api.h"
#include "../api/memtype.h"

int
btn_activ( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	char				*str;
	short				*state;
	int					*addr;
	int					fd;
	short				*selected;
	mem_data_t			data;
	
	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (widget, Pt_ARG_TEXT_STRING, &str, 0);
	PtGetResource (widget, Pt_ARG_ONOFF_STATE, &state, 0);
	PtGetResource (ABW_Num1, Pt_ARG_NUMERIC_VALUE, &addr, 0);
	PtGetResource (ABW_Combo1, Pt_ARG_CBOX_SEL_ITEM, &selected, 0);
	
	fd = open("/dev/sfins", O_RDONLY);
	if (fd == -1) {
		perror(NULL);
		return (-1);
	}
	
	switch (*selected) {
		case 1:	 data.code   = CIO_BIT; break;
		case 2:	 data.code   = WR_BIT;  break;
		case 3:	 data.code   = DM_BIT;  break;
		case 4:	 data.code   = TIM_WRD; break;
		case 5:	 data.code   = CNT_WRD; break;
		default: data.code   = CIO_BIT; break;
	}
	data.n_elem	 = 1;
	data.addr = ((*addr) << 8) + atoi (str);
	data.data [0]   = *state;
	devctl(fd, DCMD_WRITE_MEM, &data, sizeof (data), NULL); 

	close (fd);	
	return( Pt_CONTINUE );
}

